/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.SRV;

import entity.MHospital;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.MHospitalFacade;

/**
 *
 * @author SUNTIPARB
 */
@WebServlet(name="SRV_MHospitalDetail",
                loadOnStartup = 1,
                urlPatterns={"/SRV/MHospitalDetail",
                "/SRV/MHospitalDetail/formMHospital_Submit"})
public class SRV_MHospitalDetail extends HttpServlet {

    @EJB
    private MHospitalFacade mHospitalFacade;

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/SRV/MHospitalDetail")) {
            response.setContentType("text/html;charset=utf-8");
            String HospitalId = request.getParameter("id");
            if (!HospitalId.equals("-1")){
                List<MHospital> lMHospital;
                try {
                    //Query Java Object
                    lMHospital = mHospitalFacade.findByHospitalId(HospitalId);
                    if (!lMHospital.isEmpty()) {
                        request.setAttribute("HospitalId", HospitalId);
                        for (MHospital obj : lMHospital) {
                            request.setAttribute("txtName", obj.getName());
                            request.setAttribute("txtLocation", obj.getLocation());
                            request.setAttribute("txtHospitalLvl", obj.getHospitalLevel());
                            request.setAttribute("txtLatitude", (obj.getLatitude()!=null)?obj.getLatitude():"");
                            request.setAttribute("txtLongtitude", (obj.getLongtitude()!=null)?obj.getLongtitude():"");
                        }
                    }
                } catch (Exception ex) {
                    System.out.println("Error message" + ex.getMessage());
                } finally {
                    lMHospital = null;
                }
            }else{
                request.setAttribute("HospitalId", HospitalId);
                request.setAttribute("txtName", "");
                request.setAttribute("txtLocation", "");
                request.setAttribute("txtHospitalLvl", "");
                request.setAttribute("txtLatitude", "");
                request.setAttribute("txtLongtitude", "");
            }

            userPath = "/SRV/MHospitalDetail";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as
        // 8-bit Unicode (e.g., for Czech characters)
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();


        if (userPath.equals("/SRV/MHospitalDetail/formMHospital_Submit")) {

            String HospitalId = (request.getParameter("HospitalId") != null) ? request.getParameter("HospitalId") : "-1";
            String txtName = (request.getParameter("txtName") != null) ? request.getParameter("txtName") : "";
            String txtLocation = (request.getParameter("txtLocation") != null) ? request.getParameter("txtLocation") : "";
            String txtHospitalLvl = (request.getParameter("txtHospitalLvl") != null) ? request.getParameter("txtHospitalLvl") : "";
            String txtLatitude = (request.getParameter("txtLatitude") != null) ? request.getParameter("txtLatitude") : "";
            String txtLongtitude = (request.getParameter("txtLongtitude") != null) ? request.getParameter("txtLongtitude") : "";

            PrintWriter out = response.getWriter();
            try {
                //Query Java Object
                if (!HospitalId.equals("-1")){
                    mHospitalFacade.EditHospital(HospitalId, txtName, txtLocation, txtHospitalLvl, txtLatitude, txtLongtitude);
                }else{
                    mHospitalFacade.AddHospital(txtName, txtLocation, txtHospitalLvl, txtLatitude, txtLongtitude);
                }

            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
            }

            isDispatcher = false;
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }
}
